package com.nic.view.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.widget.ContentLoadingProgressBar;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.nic.view.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 加载更多
 * Created by Administrator on 2019/1/21.
 */

public abstract class LoadMoreRecycleAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    protected List<T> list = new ArrayList<>();
    protected Context context;
    protected boolean isLoadFinish;//是否加载结束
    private int pageSize;//每页条目数

    public LoadMoreRecycleAdapter(Context context, int pageSize) {
        this.context = context;
        this.pageSize = pageSize;
    }

    /**
     * 是否加载结束
     *
     * @return
     */
    public boolean isLoadFinish() {
        return isLoadFinish;
    }

    /**
     * 重新加载
     */
    public void setResetLoadMore() {
        isLoadFinish = false;
    }

    public LoadMoreRecycleAdapter(Context context) {
        this.context = context;
    }

    public void setList(List<T> list, boolean isLoadMore) {
        if (list == null || list.isEmpty()) {
            if (isLoadMore) {
                isLoadFinish = true;
                notifyDataSetChanged();
            }
            return;
        }
        if (isLoadMore) {
            this.list.addAll(list);
        } else {
            this.list = list;
        }
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        AdapterView.LayoutParams params = new AdapterView.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT);
        if (viewType == getItemCount()) {
            View bottom_view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_load_more, null);
            bottom_view.setLayoutParams(params);
            BottomViewHolder bottomViewHolder = new BottomViewHolder(bottom_view);
            return bottomViewHolder;
        }
        return onCreateContentViewHolder(viewGroup, viewType);
    }

    public abstract RecyclerView.ViewHolder onCreateContentViewHolder(@NonNull ViewGroup viewGroup, int viewType);

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
        if (getItemViewType(position) == getItemCount()) {

            BottomViewHolder bottomViewHolder = (BottomViewHolder) viewHolder;
            if (list.isEmpty()) {
                bottomViewHolder.tvLoadMore.setVisibility(View.GONE);
                bottomViewHolder.loadingBar.setVisibility(View.GONE);
//                bottomViewHolder.itemView.setBackgroundColor(Color.TRANSPARENT);
            } else {
//                bottomViewHolder.itemView.setBackgroundColor(ContextCompat.getColor(context, R.color.yellow1));
                if (getItemCount() < pageSize || isLoadFinish) {
                    bottomViewHolder.tvLoadMore.setVisibility(View.VISIBLE);
                    bottomViewHolder.loadingBar.setVisibility(View.GONE);
                } else {
                    bottomViewHolder.loadingBar.setVisibility(View.VISIBLE);
                    bottomViewHolder.tvLoadMore.setVisibility(View.GONE);
                }
            }

        } else {
            onBindContentViewHolder(viewHolder, position);
        }
    }

    public abstract void onBindContentViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position);

    @Override
    public int getItemCount() {
        return list.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position >= list.size()) {
            return getItemCount();
        }
        return super.getItemViewType(position);
    }

    class BottomViewHolder extends RecyclerView.ViewHolder {
        ContentLoadingProgressBar loadingBar;
        TextView tvLoadMore;

        public BottomViewHolder(@NonNull View itemView) {
            super(itemView);
            loadingBar = itemView.findViewById(R.id.loadingBar);
            tvLoadMore = itemView.findViewById(R.id.tv_load_more);
        }
    }
}
